package st.business.tepth.business.taskallocation.detail;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.base.OnItemChooseListener;
import st.business.tepth.business.event.ReloadLocationEvent;
import st.business.tepth.manager.UserManager;
import st.shadow.library.core.base.mvp.MvpFragment;
import st.shadow.library.core.util.utilcode.util.ScreenUtils;
import st.shadow.library.core.util.utilcode.util.ToastUtils;
import st.shadow.library.ui.dialog.DialogLoader;
import st.shadow.library.ui.dialog.IDialogClickListener;
import st.shadow.library.ui.recycler.MultipleItemEntity;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-02-26 11:59
 * 具体任务分配
 */

public class AllocationDetailDelegate extends MvpFragment<AllocationDetailPresenter> implements AllocationDetailContract.View {
    public static AllocationDetailDelegate newInstance(int repairId) {
        Bundle bundle = new Bundle();
        bundle.putInt("repairId", repairId);
        AllocationDetailDelegate delegate = new AllocationDetailDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R2.id.tv_tile)
    TextView mTvTitle;
    private AllocationDetailAdapter mAllocationDetailAdapter;
    private int mRepairId;
    private DialogLoader mDialogLoader;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mRepairId = bundle.getInt("repairId");
        }
    }

    @Override
    public AllocationDetailPresenter getPresenter() {
        return new AllocationDetailPresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_task_allcation_detail;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mTvTitle.setText("分配任务");
        mRecyclerView.setLayoutManager(new GridLayoutManager(_mActivity, 6));
        RecyclerView.ItemAnimator itemAnimator = mRecyclerView.getItemAnimator();
        if (itemAnimator != null) {
            itemAnimator.setChangeDuration(0);
        }
        mAllocationDetailAdapter = new AllocationDetailAdapter(
                null, new AllocationDetailAdapter.OnCancelListener() {
            @Override
            public void onCancel(final MultipleItemEntity multipleItemEntity,
                                 final int position) {
                mDialogLoader.showWarning("取消该作业人分配", "请确认",
                        new IDialogClickListener() {
                            @Override
                            public void onClick() {
                                mPresenter.cancelAllocate(UserManager.getInstance().getToken(),
                                        (int) multipleItemEntity.getField("classId"),
                                        (int) multipleItemEntity.getField("personId")
                                        , position);
                                mDialogLoader.changeToProgressType("正在提交");
                            }
                        });
            }
        }, new OnItemChooseListener<Integer>() {
            @Override
            public void onChoose(final Integer integer) {
                DialogChooseOtherPerson dialogChooseOtherPerson = new
                        DialogChooseOtherPerson(_mActivity, new OnItemChooseListener<List<MultipleItemEntity>>() {
                    @Override
                    public void onChoose(List<MultipleItemEntity> list) {
                        mAllocationDetailAdapter.addNewData(integer, list);
                    }
                });
                Window dialogWindow = dialogChooseOtherPerson.getWindow();
                if (dialogWindow != null) {
                    WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                    lp.width = ScreenUtils.getScreenWidth() * 1000 / 1280; // 宽度
                    lp.height = ScreenUtils.getScreenHeight() * 820 / 1024; // 高度
                    dialogWindow.setGravity(Gravity.CENTER);
                    dialogWindow.setAttributes(lp);
                    dialogChooseOtherPerson.show();
                }
            }
        });

        mAllocationDetailAdapter.setSpanSizeLookup(new BaseQuickAdapter.SpanSizeLookup() {
            @Override
            public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
                MultipleItemEntity entity = mAllocationDetailAdapter.getItem(position);
                return entity == null ? 6 : (entity.getItemType() == AllocationDetailAdapter.TYPE_1 ?
                        1 : 6);
            }
        });
        mRecyclerView.setAdapter(mAllocationDetailAdapter);
        mAllocationDetailAdapter.setEmptyView(R.layout.view_loading,
                (ViewGroup) mRecyclerView.getParent());
        mDialogLoader = new DialogLoader(_mActivity);
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        mPresenter.getTaskList(UserManager.getInstance().getToken(),
                mRepairId);
    }

    @OnClick(R2.id.tv_confirm)
    public void onConfirm() {
        String data = mAllocationDetailAdapter.getCheckedList();
        if (data != null) {
            if ("竣检员不能选多个!".equals(data)) {
                ToastUtils.showShort(data);
            } else {
                mPresenter.allocationTasks(UserManager.getInstance().getToken(), mRepairId,
                        data);
                mDialogLoader.showProgressDialog(false, "正在提交", "请稍候...");
            }
        } else {
            ToastUtils.showShort("请先勾选分配人!");
        }
    }

    @OnClick({R2.id.tv_back})
    public void onBack() {
        pop();
    }

    @Override
    public void showTaskList(List<MultipleItemEntity> list) {
        mAllocationDetailAdapter.setEmptyView(R.layout.view_search_empty,
                (ViewGroup) mRecyclerView.getParent());
        mAllocationDetailAdapter.setNewData(list);
    }

    @Override
    public void showError(String msg) {
        mDialogLoader.showFailed(msg, null, new IDialogClickListener() {
            @Override
            public void onClick() {
                mDialogLoader.clearDialog();
            }
        });
    }

    @Override
    public void showAllocationSucceed() {
        mDialogLoader.stopProgressWithSuccess("分配成功", "请确认", new IDialogClickListener() {
            @Override
            public void onClick() {
                mDialogLoader.clearDialog();
                EventBus.getDefault().post(new ReloadLocationEvent());
                pop();
            }
        });
    }

    @Override
    public void cancelAllocateSucceed(final int position) {
        mDialogLoader.stopProgressWithSuccess("取消成功", null, new IDialogClickListener() {
            @Override
            public void onClick() {
                mDialogLoader.clearDialog();
                if (mAllocationDetailAdapter != null) {
                    mAllocationDetailAdapter.cancelCheck(position);
                }
            }
        });
    }
}